package com.sdhs.paas.flux.app;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/hi")
@Slf4j
public class TestController {
	private String fake(String str) {
		log.info("fake start ...");
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("fake end ...");
		return str;
	}
	
	@GetMapping("/one")
	public String get1(){
		log.info("---start----------");
		System.out.println("start");
		String result = fake("servlet");
		log.info("end");
		System.out.println("end");
		return result;
	}
	
	@GetMapping("/two")
	public Mono<String> get2(){
		log.info("start");
		Mono<String> str =  Mono.fromSupplier(() -> fake("servlet"));
		log.info("end");
		return str;
	}
	
	@GetMapping(value="three",produces=MediaType.TEXT_EVENT_STREAM_VALUE)
	public Flux<String> three(){
		return Flux.fromStream(IntStream.range(1, 10)
				.mapToObj(i -> {
				try {
					TimeUnit.SECONDS.sleep(1);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return "result"+i;
				}
				));
	}
}
